package com.itqf.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * projectName: mybatisgp8
 *
 * @author: 赵伟风
 * time: 2021/7/30 10:13
 * description:将字符串类型转成整数数组
 */

/**
 * 数据库对应的类型
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
/**
 * java对应的类型
 */
@MappedTypes(value = Integer[].class)
public class StringToIngterArrayTypeHandler extends BaseTypeHandler<Integer[]> {

    /**
     * jackson提供的 字符串和数组类型转化的工具类
     */
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 作用: 插入/修改 数据的时候,执行java类型转成数据库类型
     * @param ps
     * @param i  占位符的位置
     * @param roleIds 插入或者修改的java类型
     * @param jdbcType 数据库对应的jdbc类型
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Integer[] roleIds, JdbcType jdbcType) throws SQLException {

        try {
            //1.将java数据转成数据需要的数据类型 [数组转成字符串]
            String roleIdsStr = objectMapper.writeValueAsString(roleIds);
            System.out.println("roleIdsStr = " + roleIdsStr);

            //2.设置占位符
            ps.setObject(i,roleIdsStr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


    }

    /**
     * 获取数据类型转成java类型
     *
     * @param rs         the rs
     * @param columnName Colunm name, when configuration <code>useColumnLabel</code> is <code>false</code>
     * @return the nullable result
     * @throws SQLException the SQL exception
     */
    @Override
    public Integer[] getNullableResult(ResultSet rs, String columnName) throws SQLException {

        //1.获取数据数据
        String jdbcRes = rs.getString(columnName);

        Integer[] roleIds = null;
        try {
            roleIds = objectMapper.readValue(jdbcRes, Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return roleIds;
    }

    @Override
    public Integer[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

        //1.获取数据数据
        String jdbcRes = rs.getString(columnIndex);

        Integer[] roleIds = null;
        try {
            roleIds = objectMapper.readValue(jdbcRes, Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return roleIds;
    }

    @Override
    public Integer[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {

        //1.获取数据数据
        String jdbcRes = cs.getString(columnIndex);

        Integer[] roleIds = null;
        try {
            roleIds = objectMapper.readValue(jdbcRes, Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return roleIds;
    }
}
